package com.plum.admin.modules.sys.controller;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.plum.admin.common.util.R;
import com.plum.admin.modules.sys.entity.SysLog;
import com.plum.admin.modules.sys.service.ISysLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import com.plum.admin.base.BaseController;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 系统日志 前端控制器
 * </p>
 *
 * @author 子丶沫
 * @since 2021-08-13
 */
@RestController
@RequestMapping("/sys/log")
@Api(tags = "日志管理")
@RequiredArgsConstructor
public class SysLogController extends BaseController {
    private final ISysLogService logService;
    @GetMapping
    @PreAuthorize("hasAnyAuthority('sys:log:list')")
    @ApiOperation("日志列表")
    public R<Object> logList(Integer page, Integer pageSize, SysLog log){
        LambdaQueryWrapper<SysLog> queryWrapper = Wrappers.<SysLog>lambdaQuery().orderByDesc(SysLog::getCreateTime);
        queryWrapper.eq(ObjectUtil.isNotNull(log.getType()),SysLog::getType,log.getType());
        return R.ok(logService.page(new Page<>(page,pageSize),queryWrapper));
    }

    @DeleteMapping
    @PreAuthorize("hasAnyAuthority('sys:log:delete')")
    @ApiOperation("删除日志")
    public R<Object> deleteLog(String ids){
        List<String> idList = Arrays.stream(ids.split(",")).collect(Collectors.toList());
        return R.ok(logService.removeByIds(idList));
    }
}
